package org.dromara.wmscloud.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.wmscloud.domain.vo.BaseMatImportVo;
import org.dromara.wmscloud.listener.BaseMatImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseMatVo;
import org.dromara.wmscloud.domain.bo.BaseMatBo;
import org.dromara.wmscloud.service.IBaseMatService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料主数据
 *
 * @author hou
 * @date 2025-04-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/mat")
public class BaseMatController extends BaseController {

    private final IBaseMatService baseMatService;

    /**
     * 查询物料主数据列表
     */
    @SaCheckPermission("base:mat:list")
    @GetMapping("/list")
    public TableDataInfo<BaseMatVo> list(BaseMatBo bo, PageQuery pageQuery) {
        return baseMatService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物料主数据
     */
    @SaCheckPermission("base:mat:list")
    @GetMapping("/listAllMat")
    public R<List<BaseMatVo>> listAll() {
        BaseMatBo bo = new BaseMatBo();
        return R.ok(baseMatService.queryList(bo));
    }

    /**
     * 导出物料主数据列表
     */
    @SaCheckPermission("base:mat:export")
    @Log(title = "物料主数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseMatBo bo, HttpServletResponse response) {
        List<BaseMatVo> list = baseMatService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料主数据", BaseMatVo.class, response);
    }

    /**
     * 获取物料主数据详细信息
     *
     * @param matId 主键
     */
    @SaCheckPermission("base:mat:query")
    @GetMapping("/{matId}")
    public R<BaseMatVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long matId) {
        return R.ok(baseMatService.queryById(matId));
    }

    /**
     * 新增物料主数据
     */
    @SaCheckPermission("base:mat:add")
    @Log(title = "物料主数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseMatBo bo) {
        return toAjax(baseMatService.insertByBo(bo));
    }

    /**
     * 修改物料主数据
     */
    @SaCheckPermission("base:mat:edit")
    @Log(title = "物料主数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseMatBo bo) {
        return toAjax(baseMatService.updateByBo(bo));
    }

    /**
     * 删除物料主数据
     *
     * @param matIds 主键串
     */
    @SaCheckPermission("base:mat:remove")
    @Log(title = "物料主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{matIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] matIds) {
        return toAjax(baseMatService.deleteWithValidByIds(List.of(matIds), true));
    }
    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "物料主数据", businessType = BusinessType.IMPORT)
    @SaCheckPermission("base:mat:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<BaseMatImportVo> result = ExcelUtil.importExcel(file.getInputStream(), BaseMatImportVo.class, new BaseMatImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", BaseMatImportVo.class, response);
    }
}
